<?php

namespace App\Http\Controllers;
use App\Models\News;
use Illuminate\Http\Request;
use Carbon\Carbon;

class NewsEditController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request,String $id)
    {
        //
        $file = $request->file('file');
        if($file) {
            $url = $request->input('loginname') . rand(1,9999) . $file->getClientOriginalName();
            $file->storeAs('img', $url, 'public');
            $imageUrl = asset('storage/img/' . $url);
        } else {
            $imageUrl = '';
        }
        News::where('id','=',$id) -> update([
            'newstitle' => $request->input('newstitle'),
            'image' => $imageUrl,
            'newscontent' => $request->input('newscontent'),
            'loginname' => $request->input('loginname'),
            'isdelete' => 0,
            'updated_at'=>Carbon::now(),
        ]);
        return response()->json(['code' => 200, 'msg' => '修改成功']);
    }
}
